﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using LibKetoan;
using System.Data;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmLogin.
	/// </summary>
	public class frmKiemtrasd : System.Windows.Forms.Form
	{
		private AccessData a;
		private string mMmyy;
		private int i_userid;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.NumericUpDown mm;
		private System.Windows.Forms.NumericUpDown yyyy;
		private System.Windows.Forms.Button butOk;
		private System.Windows.Forms.Button butCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmKiemtrasd(AccessData acc,string mmyy,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;
			i_userid=userid;
			mm.Value=decimal.Parse(mmyy.Substring(0,2));
			yyyy.Value=decimal.Parse("20"+mmyy.Substring(2,2));
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmKiemtrasd));
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.mm = new System.Windows.Forms.NumericUpDown();
			this.yyyy = new System.Windows.Forms.NumericUpDown();
			this.butOk = new System.Windows.Forms.Button();
			this.butCancel = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.mm)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.yyyy)).BeginInit();
			this.SuspendLayout();
			// 
			// label4
			// 
			this.label4.BackColor = System.Drawing.Color.Transparent;
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.ForeColor = System.Drawing.SystemColors.ControlText;
			this.label4.Location = new System.Drawing.Point(15, 20);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(42, 20);
			this.label4.TabIndex = 0;
			this.label4.Text = "Tháng :";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label5
			// 
			this.label5.BackColor = System.Drawing.Color.Transparent;
			this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.ForeColor = System.Drawing.SystemColors.ControlText;
			this.label5.Location = new System.Drawing.Point(105, 20);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(35, 20);
			this.label5.TabIndex = 2;
			this.label5.Text = "Năm :";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// mm
			// 
			this.mm.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.mm.Location = new System.Drawing.Point(65, 20);
			this.mm.Maximum = new System.Decimal(new int[] {
															   12,
															   0,
															   0,
															   0});
			this.mm.Minimum = new System.Decimal(new int[] {
															   1,
															   0,
															   0,
															   0});
			this.mm.Name = "mm";
			this.mm.Size = new System.Drawing.Size(35, 22);
			this.mm.TabIndex = 1;
			this.mm.Value = new System.Decimal(new int[] {
															 1,
															 0,
															 0,
															 0});
			this.mm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mm_KeyDown);
			// 
			// yyyy
			// 
			this.yyyy.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.yyyy.Location = new System.Drawing.Point(140, 20);
			this.yyyy.Maximum = new System.Decimal(new int[] {
																 3004,
																 0,
																 0,
																 0});
			this.yyyy.Minimum = new System.Decimal(new int[] {
																 2004,
																 0,
																 0,
																 0});
			this.yyyy.Name = "yyyy";
			this.yyyy.Size = new System.Drawing.Size(50, 22);
			this.yyyy.TabIndex = 3;
			this.yyyy.Value = new System.Decimal(new int[] {
															   2004,
															   0,
															   0,
															   0});
			this.yyyy.KeyDown += new System.Windows.Forms.KeyEventHandler(this.yyyy_KeyDown);
			// 
			// butOk
			// 
			this.butOk.Image = ((System.Drawing.Bitmap)(resources.GetObject("butOk.Image")));
			this.butOk.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butOk.Location = new System.Drawing.Point(37, 59);
			this.butOk.Name = "butOk";
			this.butOk.Size = new System.Drawing.Size(68, 25);
			this.butOk.TabIndex = 4;
			this.butOk.Text = "      Đồng ý";
			this.butOk.Click += new System.EventHandler(this.butOk_Click);
			// 
			// butCancel
			// 
			this.butCancel.Image = ((System.Drawing.Bitmap)(resources.GetObject("butCancel.Image")));
			this.butCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butCancel.Location = new System.Drawing.Point(109, 59);
			this.butCancel.Name = "butCancel";
			this.butCancel.Size = new System.Drawing.Size(68, 25);
			this.butCancel.TabIndex = 5;
			this.butCancel.Text = "Kết thúc";
			this.butCancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
			// 
			// frmKiemtrasd
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(214, 115);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butCancel,
																		  this.butOk,
																		  this.mm,
																		  this.yyyy,
																		  this.label5,
																		  this.label4});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmKiemtrasd";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Kiểm tra số dư cuối tháng";
			this.Load += new System.EventHandler(this.frmKiemtrasd_Load);
			((System.ComponentModel.ISupportInitialize)(this.mm)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.yyyy)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion


		private void mm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");		
		}

		private void yyyy_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void frmKiemtrasd_Load(object sender, System.EventArgs e)
		{
		
		}

		private void butCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void butOk_Click(object sender, System.EventArgs e)
		{
			butCancel.Enabled=butOk.Enabled=false;
			mMmyy=mm.Value.ToString().PadLeft(2,'0')+yyyy.Value.ToString().PadLeft(4,'0').Substring(2,2);			
			//lay thang tu
			string sql="",s_yy="",sMmyy_tu="";
			s_yy=yyyy.Value.ToString().Trim().Substring(2,2);
			sql="select mmyy from (select *  from a_dtsd where substr(trim(mmyy),3,4)='"+s_yy+"' and substr(mmyy,1,2)<='"+mMmyy.Substring(0,2)+"' order by mmyy asc) where rownum=1";
			DataSet ds=new DataSet();
			ds=a.get_data(sql);
			if(ds!=null && ds.Tables[0].Rows.Count>0)
				sMmyy_tu=ds.Tables[0].Rows[0]["mmyy"].ToString();
			else sMmyy_tu=mMmyy;			
			if (!a.bYyyy(yyyy.Value.ToString().PadLeft(4,'0')))
			{
				MessageBox.Show("Số liệu năm "+yyyy.Value.ToString().PadLeft(4,'0')+" chưa tạo !",a.Msg);
				return;
			}
			string s=a.KiemtraFb(sMmyy_tu,mMmyy);
			if (s!="")
			{
				MessageBox.Show("Số liệu tháng :"+s.Substring(0,s.Length-1)+" Năm "+yyyy.Value.ToString().PadLeft(4,'0')+" bị khóa !");    		
				return;
			}
			else
			{
				a.updSodu(mMmyy,a.Mmyy_sau(mMmyy),i_userid,false);
			}			
			MessageBox.Show("Đã kiểm tra số dư cuối tháng "+mMmyy.Substring(0,2)+" năm 20"+mMmyy.Substring(2,2)+" !",a.Msg);			
			butCancel.Enabled=butOk.Enabled=true;
		}
	}
}
